<template>
    <el-dialog
        title="新建"
        :visible.sync="show"
        :before-close="handleCancel"
        :close-on-click-modal="false"
        width="600px"
    >
        <div v-if="reset" v-loading="loadingGet">
            <el-form :model="formData" :rules="rules" ref="form">
                <el-form-item label="应用名称：" prop="name" label-width="100px">
                    <el-input v-model="formData.name"></el-input>
                </el-form-item>
                <el-form-item label="应用代码：" prop="code" label-width="100px">
                    <el-input v-model="formData.code"></el-input>
                </el-form-item>
                <el-form-item label="应用地址：" prop="url" label-width="100px">
                    <el-input v-model="formData.url"></el-input>
                </el-form-item>
                <el-form-item label="应用描述：" prop="description" label-width="100px">
                    <el-input type="textarea" v-model="formData.description"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button @click="handleCancel" size="small">取消</el-button>
            <el-button type="primary" @click="handleSubmit" size="small" :loading="loadingSubmit">确定</el-button>
        </div>
    </el-dialog>
</template>

<script>
import sys_application_api from '../../api/sys_application_api'

export default {
    props: {
        show: Boolean,
        success: Function,
        cancel: Function,
    },
    created() {
    },
    watch: {
        show(val) {
            if (val) {
                this.reset = false
                this.$nextTick(() => {
                    Object.assign(this.$data, this.$options.data.call(this))
                    this.reset = true
                    this.init && this.init()
                })
            }
        }
    },
    data() {
        return {
            reset: true,
            loadingGet: false,
            loadingSubmit: false,
            formData: {},
            rules: {
                name: [
                    { required: true, message: '请输入应用名称', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入应用代码', trigger: 'blur' }
                ],
                url: [
                    { required: true, message: '请输入应用地址', trigger: 'blur' }
                ],
                description: [
                    { max:100, message: '不要超过100个字符', trigger: 'blur' }
                ],
            },
        }
    },
    methods: {
        handleSubmit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loadingSubmit = true
                    sys_application_api.add(this.formData).then(data => {
                        this.loadingSubmit = false
                        this.$message.success('保存成功!')
                        this.success()
                    }).catch(err => {
                        this.loadingSubmit = false
                        this.$message.error(err.message)
                    })
                }
            })
        },
        handleCancel() {
            this.cancel()
        },
    }
}
</script>

<style scoped>
</style>
